package turtle.system.pt;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.ListIterator;
import turtle.system.util.techindex.*;

public class BarSet {
	private String stockCode;
	private String stockName;
	public static boolean ADX_ATTACH = false;
	private ArrayList<Bar> list = new ArrayList<Bar>();
	
	public ListIterator<Bar> iter() {
		return list.listIterator();
	}
	public ListIterator<Bar> iter(int index) {
		return list.listIterator(index);
	}
	public int size() {
		return list.size();
	}
	public Bar getBar(int index) {
		return list.get(index);
	}
	
	public String getStockCode() { return stockCode; }
	public String getStockName() { return stockName; }
	
	public void attachADX() {
		ADXIndex.attachIndex4Bar(list);
	}
	public BarSet(InputStreamReader isr) {
		BufferedReader br = new BufferedReader(isr);
		String str;
		double prevClose = 0;
		boolean firstBar = true;
		try {
			while ((str = br.readLine()) != null) {
				String[] sa = str.split("[\\s]+");
				if (sa.length == 2) {
					stockCode = sa[0];
					System.out.println("stockCode = [" + stockCode + "]");
					stockName = sa[1];
					System.out.println("stockName = [" + stockName + "]");
					
				} else if (sa.length == 6) {
					if (!Character.isDigit((sa[0].charAt(0)))) continue;
					Bar bar = new Bar(sa);
					if (firstBar) { prevClose = bar.close; firstBar = false; continue; }
					bar.diff = bar.close - prevClose;
					list.add(bar);
					prevClose = bar.close;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (ADX_ATTACH)
		  attachADX();
	}
	

}
